<template>
  <header class="header">
    <h1>todos</h1>
    <input id="toggle-all" class="toggle-all" type="checkbox" v-model="isAll" />
    <label for="toggle-all"></label>
    <input
      class="new-todo"
      placeholder="输入任务名称-回车确认"
      autofocus
      v-model.trim="name"
      @keyup.enter="add"
    />
  </header>
</template>

<script>
import { nanoid } from 'nanoid'
export default {
  data() {
    return {
      name: ''
    }
  },
  props: ['list'],
  methods: {
    add() {
      if (this.name.length == 0) return alert('请输入内容!')
      let info = { id: nanoid(), name: this.name, isDone: false }
      this.$emit('addList', info)
      this.name = ''
    }
  },
  computed: {
    isAll: {
      get() {
        if (this.list.length === 0) return false
        return this.list.every((item) => item.isDone)
      },
      set(isAll) {
        this.$emit('selectAll', isAll)
      }
    }
  }
}
</script>
